<!--
 * @Author: jlzm
 * @Date: 2020-03-02 10:49:55
 * @LastEditors: jlzm
 * @LastEditTime: 2020-07-02 11:17:07
 * @FilePath: \MPShop\mixins\common.vue
--> 
<script>
const app = getApp();
import {
	baseShopId,  //店铺id
	baseFileUri,
	templateFileUri,
	baseTemplateData
} from "@/utils/request/baseConfig";
import { mapState } from "vuex";

import { setHasNetwork, getHasNetwork } from "@/lib/storage";
/* 主题样式的基本参数 */
const themeStyles = baseTemplateData.themeStyles;
/* 对应模板和主题的静态资源路径 */
const baseTemplateFileUri = `${templateFileUri}${baseTemplateData.templateId}/${baseTemplateData.themeStyles.themeId}/`;
/* 通用的静态资源路径 */
const baseCommonFileUri = `${templateFileUri}common/`;
console.log("baseTemplateFileUri", baseTemplateFileUri);
console.log("baseCommonFileUri", baseCommonFileUri);
/*  */
import {
	_getSystemConfigValue
} from "@/lib/api/operation-server/info"
/** 存储网络状态 begin */

// const getNetWorkStatus = () => {
// 	setHasNetwork();
// 	uni.onNetworkStatusChange(res => {
// 		console.log("res", res);
// 		setHasNetwork(res.isConnected);
// 	});
// };

// getNetWorkStatus();

/** 存储网络状态 end */

export default {
	data() {
		return {
			checkoutNetwork: true,
			loaded: false, // 页面数据加载是否完毕
			baseShopId, //店铺id
			baseFileUri,
			noData: false,
			loadingType: "more",
			baseTemplateFileUri, // 模板静态资源路径
			baseCommonFileUri, //  公共模板静态资源路径
			addKeyboardInput:true, //输入组件控制
			templateId: baseTemplateData.templateId, // 模板Id
			/** 主题 begin */
			themeStyles: {
				mainColor: themeStyles.mainColor, // 主色
				lightMainColor: themeStyles.lightMainColor, // 主衬色
				auxiliaryColor: themeStyles.auxiliaryColor, // 辅色
				lightAuxiliaryColor: themeStyles.lightAuxiliaryColor, // 辅衬色
				adronmentColor: themeStyles.adronmentColor, // 点缀色
				shadowCss:`0px 12rpx 28rpx 0px ${themeStyles.mainColor}66`,
				backgroundMainColor: `background-color: ${themeStyles.mainColor};`,
				textMainColor: `color: ${themeStyles.mainColor};`,
				textLightMainColor: `color: ${themeStyles.lightMainColor};`,
				textAuxiliaryColor: `color: ${themeStyles.auxiliaryColor};`,
				textAdronmentColor: `color: ${themeStyles.adronmentColor};`,
				btnFullColor: `color: ${themeStyles.auxiliaryColor};background-color: ${themeStyles.mainColor}`,
				btnAuxiliaryColor: `color: ${themeStyles.lightMainColor};border-color: ${themeStyles.lightMainColor}`,
				btnGhostColor: `color: ${themeStyles.mainColor};border-color: ${themeStyles.mainColor};background-color: ${themeStyles.lightAuxiliaryColor}`,
				btnLightAuxiliaryColor: `color: ${themeStyles.auxiliaryColor};background-color: ${themeStyles.mainColor}B3`,
				/* 按钮失效 */
				btnDisabled:`color:${themeStyles.auxiliaryColor};background-color:${themeStyles.disabledColor};`,
				/* 确认按钮 */
				btnPrimary:`color:${themeStyles.auxiliaryColor};background-color: ${themeStyles.mainColor};border-color:${themeStyles.mainColor};`,
				/* 按钮选中 */
				btnSelected:`color:${themeStyles.mainColor};border-color:${themeStyles.mainColor};`,
				/* 阴影 */
				boxShadow:`box-shadow:0px 12rpx 28rpx 0px ${themeStyles.mainColor}66;`,
				/* 按钮-商品仅展示 */
				btnJustShow:`color:#333;background-color:#ccc`
				/*  */
				// mainColor: themeStyles.mainColor, // 主色
				// lightMainColor: themeStyles.lightMainColor, // 主衬色
				// auxiliaryColor: themeStyles.auxiliaryColor, // 辅色
				// lightAuxiliaryColor: themeStyles.lightAuxiliaryColor, // 辅衬色
				// adronmentColor: themeStyles.adronmentColor, // 点缀色
				// backgroundMainColor: `background-color: ${themeStyles.mainColor};`,
				// textMainColor: `color: ${themeStyles.mainColor};`,
				// textAuxiliaryColor: `color: ${themeStyles.auxiliaryColor};`,
				// textAdronmentColor: `color: ${themeStyles.adronmentColor};`,
				// btnFullColor: `color: ${themeStyles.auxiliaryColor};background-color: ${themeStyles.mainColor}`,
				// btnAuxiliaryColor: `color: ${themeStyles.mainColor};border-color: ${themeStyles.mainColor}`,
				// btnGhostColor: `color: ${themeStyles.mainColor};border-color: ${themeStyles.mainColor};background-color: ${themeStyles.lightAuxiliaryColor}`,
				// btnLightAuxiliaryColor: `color: ${themeStyles.lightAuxiliaryColor};background-color: ${themeStyles.mainColor}B3`
			}
			/** 主题 end */
		};
	},

	computed: {
		...mapState("login", ["hasLogin", "userInfo", "accountInfo"])
	},

	onLoad() {
		
		// if (this.checkoutNetwork && !getHasNetwork()) {
		// 	console.log("无网络~");
		// 	this.navTo("/pages/common/pages/noNetwork/noNetwork");
		// 	return;
		// }
	},

	onShow() {
		setHasNetwork();
	},

	methods: {
		navTo(url) {
			if(!url){
				this.$api.showToast("暂未开放");
				return;
			}
			uni.navigateTo({ url });
		},

		switchTo(url) {
			uni.switchTab({ url });
		},

		/**
		 * 预览图片
		 * @param { Array } urls    // 图片数组
		 * @param { Number } current    // 当前预览索引
		 */
		previewImage(urls = [], current) {
			uni.previewImage({
				urls,
				current
			});
		},

		// 需登录跳转
		loginNavTo(url = "") {
			console.log("url", url);
			if (!this.hasLogin) {
				uni.navigateTo({ url: "/pages/common/pages/login/login" });
				return;
			}
			url && this.navTo(url);
		},
		/* 去掉省市??? */
		ZZremove_PCD_str(str){
			if(str){
				let regx = /^(([\u4e00-\u9fa5]+省)|([\u4e00-\u9fa5]+自治区)|([\u4e00-\u9fa5]+特别行政区))([\u4e00-\u9fa5]+市)/;
				return str.replace(regx,"");
			}else{
				return "";
			}
		},
		/* 页面返回顶部 */
		ZZscrollTop(){
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 100
			});
		},
		/* 数字.00补零 */
		ZZaddNumZero(num){
			try {
				let numStr = String(num),
					regx = /\.\d{1,}$/g;
				//console.log(numStr.match(regx));
				let matchStr = numStr.match(regx);
				if(matchStr===null){
					return `${num}.00`;
				}
				if(matchStr[0].length===2){
					return `${num}0`;
				}
				if(matchStr[0].length>2){
					return `${num}`;
				}
			} catch (error) {
				return "0.00";
			}
		},
		/* 空对象 */
		ZZisEmptyObject(obj){
			for(const key in obj){
				if(obj.hasOwnProperty(key)){
					return false;
				}
			}
			return true;
		},
		/* 获取用户位置 */
		ZZgetLocation(){
			return new Promise((resolve,reject)=>{
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
						//console.log(res);
						resolve(res);
					},
					fail:err=>{
						//console.log(err);
						reject({
							notGetLocation:true
						});
					}
				});
			});
		},
		/*  */
		ZZopenLocation(lat,lng){
			uni.openLocation({
				latitude: lat,
				longitude: lng,
				success:res=>{
					//console.log(res);
				},
				fail:err=>{

				}
        	});
		},
		/*  */
		ZZgetSystemConfigValue({data = ""}){
			return _getSystemConfigValue(arguments[0]);
		},
		/*  */
		ZZsetTimeout(fn,time){
			let timer = null;
			clearTimeout(timer);
			timer = setTimeout(fn.bind(this),time);
		},
		/* 打电话 */
		ZZmakePhoneCall(phone){
			uni.makePhoneCall({
    			phoneNumber: `${phone}`
			});
		},
		stopBubble() {}
	}
};
</script>
